<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class MenuController extends BaseController{
    public function show(){
        $d=array();
        $pages = ceil(M('menus')->count()/4);
        $a=M('menus')-> page(I('get.p'),4)->select();
        $b=M('menus')->max('mgroup');
        $c=M('menus')->select();
        foreach($c as $v){
            $d[$v['menu_id']]=$v['mname'];
        }
        $d[0]='无';
        $e=M('menus')->where(array('up'=>0))->select();
        $this->assign('upselect',$e);
        $this->assign('upselects',$e);
        $this->assign('upname',$d);
        $this->assign('maxgp',$b);
        $this->assign('navs',$a);
        $this->assign('addnavs',$c);
        $this->assign('pages',$pages);
        $this->display();
    }
    public function getShow(){
        $pages = ceil(M('menus')->count()/4);
        $res=M('menus')
            ->page(I('get.p'),4)
            ->select();
        $resa=array();
        foreach($res as $k=>$v){
            if($v['up']==0){
                $b['mname']='无';
            }else{
                $b=M('menus')->where(array('menu_id'=>"{$v['up']}"))->find();
            }
            $resa[$k]=$v;
            $resa[$k]['up']=$b['mname'];
        }
        $this->ajaxReturn($resa);
    }
    public function doDelMenu(){
        M('menus')->delete(I('post.ids'));
        $res = [
            'msg'=>'删除成功',
            'error'=>0
        ];
        $this->ajaxReturn($res);
    }
    public function doEditMenu(){
        if(D('menus')->create()){
            D('menus') ->save();
            $res = [
            'msg'=>'修改成功',
            'error'=>0
        ];
        }else{
            $res = [
                'msg'=>D('menus')->getError(),
                'error'=>1,
            ];
        }
        $this->ajaxReturn($res);
    }
    public function doAddMenu(){
        if(D('menus')->create()){
            D('menus') ->add();
            $res = [
                'msg'=>'添加成功',
                'error'=>0
            ];
        }else{
            $res = [
                'msg'=>D('menus')->getError(),
                'error'=>1,
            ];
        }
        $this->ajaxReturn($res);
    }
}